<template>
  <div id="idcard">
    <van-form @submit="submit">
      <van-nav-bar title="1.实名认证" />
      <van-field
        style="margin-top: 20px"
        v-model="id_card"
        name="id_card"
        label="身份证"
        required
        placeholder="请输入"
        :rules="[
          { required: true, message: '必填' },
          { pattern: id_card_rule, message: '身份证格式错误' },
        ]"
      >
      </van-field>
      <van-field
        v-model="id_name"
        name="id_name"
        label="姓名"
        required
        placeholder="请输入"
        :rules="[{ required: true, message: '必填' }]"
      >
      </van-field>
      <van-button block native-type="submit" type="info">确定提交</van-button>
    </van-form>
  </div>
</template>

<script>
import { Dialog } from "vant";
export default {
  data() {
    return {
      id_card: "",
      id_name: "",
      id_card_rule:
        /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/,
      token: this.$store.state.Authorization,
    };
  },
  methods: {
    submit(e) {
      this.$axios({
        method: "post",
        url: "/api/adminapi/signin",
        data: {
          signin_type: "card",
          card: this.$AES.encrypt(e.id_card),
          unionid: this.$AES.encrypt(this.$store.state.unionid),
        },
      }).then((res) => {
        if (res.data.ret == 0) {
          let usertype = res.data.usertypen;
          let is_active = res.data.is_active;
          let username = res.data.username;
          let token = res.data.token;
          let unionid = this.$store.state.unionid;
          let mode = res.data.user_mode;
          // 相关信息放入缓存
          this.$store.dispatch("login", {
            username,
            token,
            usertype,
            is_active,
            unionid,
            mode,
          });
          let usertype_int = parseInt(this.$AES.decrypt(usertype));
          if (usertype_int >= 0 && mode=="1") {
            let openid = res.data.openid;
            this.$store.dispatch("resetOpenid", openid).then(() => {
              this.$router.replace("/faceadd");
            });
          }else if(usertype_int >= 0 && mode=="0"){
            let openid = res.data.openid;
            this.$store.dispatch("resetOpenid", openid).then(() => {
              this.$router.replace("/client/home");
            });
          }
          // 登录失败
        } else if (res.data.ret == 1 || res.data.ret == 3) {
          Dialog.alert({
            title: "提示",
            message: res.data.msg,
          }).then(() => {
            this.$router.replace(`/404?msg=${res.data.msg}`);
          });
        } else if (res.data.ret == 5) {
          Dialog.alert({
            title: "提示",
            message: `认证失败，${res.data.msg}`,
          });
        }
      });
    },
  },
};
</script>

<style scoped>
#idcard {
  background: #f7f8fa;
  width: 100%;
  height: 100%;
}
</style>
<style>
#idcard .van-field__label {
  width: 4em;
}
.van-uploader__upload {
  width: 200px;
  height: 266px;
}
</style>